//
//  OptionLayer.m
//  CarDemo
//
//  Created by huang kun on 8/9/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "OptionLayer.h"
#import "CarDemoAppDelegate.h"
#import "SoundMgr.h"
#import "MenuLayer.h"
#import "SimpleAudioEngine.h"

@implementation OptionLayer

@synthesize BGMVolume;
@synthesize SEVolume;
@synthesize vibrate;

- (void) dealloc {
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}

- (void) setVibrate:(BOOL)on {
	[SoundMgr playSound:ksoundButton];
	
	
	vibrate = on;
	CarDemoAppDelegate *delegate = (CarDemoAppDelegate *)[[UIApplication sharedApplication] delegate];
	delegate.isVibrateOn = on;
	[SoundMgr playVibrate];
	
	if(on) {
		vibrateOn.opacity = 255;
		vibrateOff.opacity = 0;
	}
	else {
		vibrateOff.opacity = 255;
		vibrateOn.opacity = 0;
	}
}

- (void) setBGMVolume:(float)volume {
	if(volume >= 0 && volume <= 1){
		BGMVolume = volume;
		[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:BGMVolume];
		BGMSprite.textureRect = CGRectMake(0, 0, optionBGMWidth*volume, optionBGMHeight); 
	}
}

- (void) setSEVolume:(float)volume {
	if(volume >= 0 && volume <= 1){
		SEVolume = volume;
		[[SimpleAudioEngine sharedEngine] setEffectsVolume:SEVolume];
		SESprite.textureRect = CGRectMake(0, 0, optionBGMWidth*volume, optionBGMHeight); 
	}
}

- (id) init {
	if( (self = [super init]) ){
		isTouchEnabled = YES;
		CGSize size = [[CCDirector sharedDirector] winSize];

		CCSprite *bg = [CCSprite spriteWithFile:optionBG];
		[self addChild:bg];
		bg.position = ccp(size.width/2, size.height/2);
		
		
		CCSpriteSheet *sheet = [CCSpriteSheet spriteSheetWithFile:soundOptionBG];
		[self addChild:sheet];
		
		
		
		BGMSprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(0, 0, optionBGMWidth, optionBGMHeight)];
		BGMSprite.anchorPoint = ccp(0, 0.5);
		[sheet addChild:BGMSprite];
		BGMSprite.position = ccp(optionBGMLeft, optionBGMPosition.y);
		
		SESprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(0, 0, optionBGMWidth, optionBGMHeight)];
		SESprite.anchorPoint = ccp(0, 0.5);
		[sheet addChild:SESprite];
		SESprite.position = ccp(optionBGMLeft, optionSEPosition.y);
		
		
		vibrateOn = [CCSprite spriteWithFile:soundOptionVibrateOn];
		[self addChild:vibrateOn];
		vibrateOn.position = vibrateOnPosition;
		
		vibrateOff = [CCSprite spriteWithFile:soundOptionVibrateOff];
		[self addChild:vibrateOff];
		vibrateOff.position = vibrateOffPosition;
		
		CCMenuItemImage *item = [CCMenuItemImage itemFromNormalImage:optionSaveButton
										  selectedImage:optionSaveButtonPressed
												 target:self selector:@selector(backToMenu:)];
		
		item.position = saveButtonPosition;
		CCMenu *menu = [CCMenu menuWithItems:item,nil];
		[self addChild:menu];
		menu.position = CGPointZero;
		
		[self defaultOption];
	}
	return self;
}

- (void) defaultOption {
	NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
	
	if( [defs objectForKey:@"BGM"] == nil){
		BOOL tempValue;
		NSString *model = [[UIDevice currentDevice] model];
		if( [model isEqualToString:@"iPhone"] ){
			tempValue = YES;	
		}
		else if( [model isEqualToString:@"iPod touch"] ){
			tempValue = NO;	
		}	
		
		NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
									[NSNumber numberWithFloat:1.0f],@"BGM",
									[NSNumber numberWithFloat:1.0f],@"SE",
									[NSNumber numberWithBool:tempValue],@"VIBRATE",nil];
		[defs registerDefaults:dictionary];
	}
	self.BGMVolume = [[defs objectForKey:@"BGM"] floatValue];
	self.SEVolume = [[defs objectForKey:@"SE"] floatValue];
	self.vibrate = [[defs objectForKey:@"VIBRATE"] boolValue];
}

- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint location = [touch locationInView:[touch view]];	
	location = [[CCDirector sharedDirector] convertToGL:location];
	
	if( CGRectContainsPoint(optionBGMRect, location) ){
		self.BGMVolume = (location.x - optionBGMLeft)/optionBGMWidth;
	}
	else if( CGRectContainsPoint(optionSERect, location) ){
		self.SEVolume = (location.x - optionBGMLeft)/optionBGMWidth;
	}
	else if( CGRectContainsPoint(vibrateOn.boundingBox, location)){
		self.vibrate = YES;
	}
	else if( CGRectContainsPoint(vibrateOff.boundingBox, location)){
		self.vibrate = NO;
	}
}

- (void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [touches anyObject];
	CGPoint location = [touch locationInView:[touch view]];	
	location = [[CCDirector sharedDirector] convertToGL:location];
	
	if( CGRectContainsPoint(optionBGMRect, location) ){
		self.BGMVolume = (location.x - optionBGMLeft)/optionBGMWidth;
	}
	else if( CGRectContainsPoint(optionSERect, location) ){
		self.SEVolume = (location.x - optionBGMLeft)/optionBGMWidth;
	}
}


- (void) backToMenu:(id)sender {
	[SoundMgr playSound:ksoundButton];

	NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
	[defs setObject:[NSNumber numberWithFloat:BGMVolume] forKey:@"BGM"];
	[defs setObject:[NSNumber numberWithFloat:SEVolume] forKey:@"SE"];
	[defs setObject:[NSNumber numberWithBool:vibrate] forKey:@"VIBRATE"];

	[[CCTextureCache sharedTextureCache] removeUnusedTextures];
	[[CCTextureCache sharedTextureCache] removeAllTextures];
	[[CCDirector sharedDirector] replaceScene:[[CCScene node] addChild:[MenuLayer node]]];
}

@end
